package com.neusoft.neuiotms.dcam.fixed.asynchronous;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AsyncCallService {
    @Value("${dcam.threadPoolSize:1}")
    private Integer threadPoolSize;

    private ExecutorService executor;

    @PostConstruct
    public void startThread() {
        this.executor = Executors.newFixedThreadPool(this.threadPoolSize);
    }

    @PreDestroy
    public void destroyExecutor() {
        this.executor.shutdown();
    }

    public void handle(final Callable<String> callback) {
        AsyncCallService.log.info("导入 线程处理开始");
        this.executor.submit(callback);
        AsyncCallService.log.info("导入 线程处理结束");
    }

    public void handleRun(final Runnable run) {
        AsyncCallService.log.info("导入 线程处理开始");
        this.executor.execute(run);
        AsyncCallService.log.info("导入 线程处理结束");
    }
}
